package com.example.deertrip;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.example.deertrip.bean.Moment;
import com.example.deertrip.bean.Tb_HotelInfo;
import com.example.deertrip.database.DBOperate;
import com.example.deertrip.tools_class.DrawableHelp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Hotel_Model_Activity extends AppCompatActivity {
    private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    private ListView listView;
    private SimpleAdapter adapter = null;
    private DBOperate dbOperate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hotel_model);


        listView=findViewById(R.id.htlist);
        dbOperate=new DBOperate(Hotel_Model_Activity.this);
        sql();
    }

    private void sql(){
        List<Tb_HotelInfo> HotelInfos= dbOperate.get_All_HotelInfo();
        List<HashMap<String, Object>> data= new ArrayList<HashMap<String, Object>>();//存放全部数据的列表
        Drawable dw;

        for(int i=0;i<HotelInfos.size();i++){
            HashMap<String, Object> items=new HashMap<String, Object>(); //存放一条数据的哈希表

            Tb_HotelInfo HotelInfo=HotelInfos.get(i);
            BitmapDrawable bd=new BitmapDrawable(this.getResources(),HotelInfo.getHotelImage());//将Test对象中读取的点阵图片转换为bitmapdrawable
            dw=bd;    //再转换为Drawable
            items.put("HotelImage", dw);   //属性分别存入哈希表
            items.put("HotelAddress", HotelInfo.getHotelAddress());
            items.put("HotelScore", HotelInfo.getHotelScore());
            items.put("HotelName", HotelInfo.getHotelName());
            items.put("HotelTel", HotelInfo.getHotelTel());
            data.add(items);   //总数据哈希表中加入该条数据项
        }


        adapter = new SimpleAdapter(Hotel_Model_Activity.this, data, R.layout.hotel_info_bar,
                new String[]{"HotelImage","HotelAddress", "HotelScore","HotelName","HotelTel"}, new int[]{R.id.htimage,R.id.htaddress, R.id.htscore,R.id.htname, R.id.httel});

        adapter.setViewBinder(new SimpleAdapter.ViewBinder(){
            public boolean setViewValue(View view,Object data,String textRepresentation){
                if(view instanceof ImageView && data instanceof Drawable){
                    ImageView iv=(ImageView)view;
                    iv.setImageDrawable((Drawable)data);
                    return true;
                }
                else return false;
            }
        });

        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                HashMap<String, Object> items=new HashMap<String, Object>();
                Bundle bundle=new Bundle();
                items=data.get(i);
                bundle.putString("HotelAddress",(String) items.get("HotelAddress"));
                bundle.putString("HotelScore",(String)items.get("HotelScore"));
                bundle.putString("HotelName",(String)items.get("HotelName"));
                bundle.putLong("HotelTel",(Long)items.get("HotelTel"));


                Drawable image=(Drawable) items.get("HotelImage");
                byte[] out= DrawableHelp.drawableTobyte(image);

                Intent intent=new Intent(Hotel_Model_Activity.this,HotelActivity.class);
                intent.putExtras(bundle);
                intent.putExtra("HotelImage",out);
                startActivity(intent);
            }
        });


    }
}